import axios from 'axios'
import router from '@/router/pages'
// 设置域名

let BASEURL = defaultUrl


// 配置域名、超时时间（10s）
let $ = axios.create({
  baseURL: BASEURL,
  timeout: 30000
})


// 请求拦截器
$.interceptors.request.use(
  config => {
    console.log('拦截了吗');
    // console.log("soa_account: "+sessionStorage.getItem("soa_account"))
    if (config.method == "get") {
      let soa_account = sessionStorage.getItem("soa_account");
      if (soa_account != "" && soa_account != null) {
        // config['params'] ={"soa_account":soa_account}

        config.headers.account = soa_account
      }
      //设置token的请求头
      const token = sessionStorage.getItem("soa_token")
      config.headers.token = `${token}`;
    } else {
      let soa_account = sessionStorage.getItem("soa_account");
      if (soa_account != "" && soa_account != null) {
        // config['params'] ={"soa_account":soa_account}

        config.headers.account = soa_account
      }
      //设置token的请求头
      const token = sessionStorage.getItem("soa_token")
      config.headers.token = `${token}`;
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
$.interceptors.response.use(
  response => {
    const status = response.status
    if (status == 401 || status == 403) {
      this.$message.error('没有权限')

    }
    if (response.data.code == 401 || response.data.code == 403) {
      window.location.href = "/login"
      // this.$router.push('/login')
      // alert('登陆失败，请联系管理员')
     
    }
    if (response.data.code == 405) {
      sessionStorage.setItem("soa_account", "")
      sessionStorage.setItem("soa_token", "")
      window.location.href = "/login"
      // this.$router.push('/login')
      alert('该账号无权限')
    }
    return response
  },
  error => {
    return error
  }
)

export default axios
